﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Data.ConnectionUI;

namespace Pawn
{
    public partial class FrmMain :Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(Properties.Settings.Default.PawnConnectionString);
                con.Open();
                con.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Không thể kết nối đến CSDL!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DataConnectionDialog dialog = new DataConnectionDialog();
                DataSource.AddStandardDataSources(dialog);
                dialog.SelectedDataSource = DataSource.SqlDataSource;
                dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
                if (DataConnectionDialog.Show(dialog) == DialogResult.OK)
                {
                    Properties.Settings.Default.PawnConnectionString = dialog.ConnectionString;
                    Properties.Settings.Default.Save();
                    FrmMain_Load(sender, e);
                }
                else
                    this.Close();
            }
        }
        private void OpenChild(Form frm)
        {
            foreach (Form f in this.MdiChildren)
            {
                if (f.Name == frm.Name)
                {
                    f.Activate();
                    return;
                }
            }
            frm.MdiParent = this;
            frm.Show();
        }

        private void cấuHìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmCauHinh frm = new FrmCauHinh();
            frm.ShowDialog();
        }

        private void lậpPhiếuVayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenChild(new FrmPhieuVay());
        }

        private void danhSáchPhiếuVayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenChild(new FrmDSPhieu());
        }

        private void danhSáchKháchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenChild(new FrmKhach());
        }

        private void phiếuĐãTrảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenChild(new FrmDSPhieuQH());
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var q = from c in db.CaiDats
                    select c;
            foreach (var c in q)
            {
                if (c.MatKhau.Length == 0) return;
                using (FrmDangNhap frm = new FrmDangNhap())
                {
                    if (frm.ShowDialog() != DialogResult.OK)
                        this.Close();
                }
                break;
            }
        }

        private void phiếuĐóngLãiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenChild(new FrmDSPhieuDL());
        }
    }
}
